package cn.iocoder.yudao.module.system.controller.admin.tenantapprovelist;

import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.system.convert.tenant.TenantApproveListConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.service.tenant.TenantService;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

import cn.iocoder.yudao.module.system.controller.admin.tenantapprovelist.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.tenantapprovelist.TenantApproveListDO;
import cn.iocoder.yudao.module.system.service.tenantapprovelist.TenantApproveListService;

@Tag(name = "管理后台 - 开户申请记录")
@RestController
@RequestMapping("/system/tenant-approve-list")
@Validated
public class TenantApproveListController {

    @Resource
    private TenantApproveListService tenantApproveListService;

    @Resource
    private AdminUserService adminUserService;

    @Resource
    private TenantService tenantService;

    @PostMapping("/create")
    @Operation(summary = "创建开户申请记录")
    public CommonResult<Long> createTenantApproveList(@Valid @RequestBody TenantApproveListSaveReqVO createReqVO) {
        createReqVO.setHandleStatus(0);
        return success(tenantApproveListService.createTenantApproveList(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新开户申请记录")
    public CommonResult<Boolean> updateTenantApproveList(@Valid @RequestBody TenantApproveListSaveReqVO updateReqVO) {
        tenantApproveListService.updateTenantApproveList(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除开户申请记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:tenant-approve-list:delete')")
    public CommonResult<Boolean> deleteTenantApproveList(@RequestParam("id") Long id) {
        tenantApproveListService.deleteTenantApproveList(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得开户申请记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<TenantApproveListRespVO> getTenantApproveList(@RequestParam("id") Long id) {
        TenantApproveListDO tenantApproveList = tenantApproveListService.getTenantApproveList(id);
        return success(BeanUtils.toBean(tenantApproveList, TenantApproveListRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得开户申请记录分页")
    public CommonResult<PageResult<TenantApproveListRespVO>> getTenantApproveListPage(@Valid TenantApproveListPageReqVO pageReqVO) {
        PageResult<TenantApproveListDO> pageResult = tenantApproveListService.getTenantApproveListPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TenantApproveListRespVO.class));
    }

    @GetMapping("/get-by-admin")
    @Operation(summary = "获得开户申请记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<TenantApproveListRespVO> getTenantApproveDataByAdmin(@RequestParam("id") Long id) {
        TenantApproveListDO tenantApproveList = tenantApproveListService.getTenantApproveList(id);



        return success(BeanUtils.toBean(tenantApproveList, TenantApproveListRespVO.class));
    }

    @GetMapping("/page-by-admin")
    @Operation(summary = "获得开户申请记录分页")
    @TenantIgnore
    public CommonResult<PageResult<TenantApproveListRespVO>> getTenantApproveListPageByAdmin(@Valid TenantApproveListPageReqVO pageReqVO) {
        PageResult<TenantApproveListDO> pageResult = tenantApproveListService.getTenantApproveListPage(pageReqVO);
        Map<Long, AdminUserDO> userMap = adminUserService.getUserMap(pageResult.getList().stream().map(TenantApproveListDO::getOperator).collect(Collectors.toList()));
        Map<Long, TenantDO> tenantMap = tenantService.getTenantMap(pageResult.getList().stream().map(TenantApproveListDO::getCreateTenantId).collect(Collectors.toList()));
        return success(TenantApproveListConvert.INSTANCE.convert(pageResult, tenantMap, userMap));
    }

    /**
     * 执行一键创建逻辑
     * @param id
     * @since 2025-01-08
     * @author PetePower
     * @return
     */
    @PostMapping("/handle-create")
    @Operation(summary = "一键创建租户")
    @TenantIgnore
    @PreAuthorize("@ss.hasPermission('system:tenant:create')")
    public CommonResult<Boolean> createTenantApproveList(@Valid @RequestParam("id") Long id) {
        tenantApproveListService.handleCreate(id);
        return success(true);
    }

    @PostMapping("/approve")
    @Operation(summary = "修改审批状态")
    @TenantIgnore
    @PreAuthorize("@ss.hasPermission('system:tenant:create')")
    public CommonResult<Boolean> createTenantApproveList(@Valid @RequestBody TenantApproveListApproveReqVO createReqVO) {
        createReqVO.setOperator(getLoginUserId());
        tenantApproveListService.approve(createReqVO);
        return success(true);
    }



    @GetMapping("/export-excel")
    @Operation(summary = "导出开户申请记录 Excel")
    @OperateLog(type = EXPORT)
    public void exportTenantApproveListExcel(@Valid TenantApproveListPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TenantApproveListDO> list = tenantApproveListService.getTenantApproveListPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "开户申请记录.xls", "数据", TenantApproveListRespVO.class,
                        BeanUtils.toBean(list, TenantApproveListRespVO.class));
    }

}